package jp.bsws.action.master;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import jp.bsws.bean.KeyValueBean;
import jp.bsws.bean.UriageShubetsuBean;
import jp.bsws.common.BSWSConstants;
import jp.bsws.common.BSWSSession;
import jp.bsws.common.BSWSUtils;
import jp.bsws.common.DBUtils;
import jp.bsws.dao.BSWSDao;
import jp.bsws.dao.UriageShubetsuDao;
import jp.bsws.model.UriageShubetsuModel;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/*
 * メインメニューアクションクラス
 */
public class UriageShubetsuMasterAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private BSWSSession bSWSSession;

	public String getLoginJugyoinKnj() { return bSWSSession.getStrJugyoinKnj(); }

	private List<UriageShubetsuBean> uriageShubetsuBeanList;
	private Integer intTaisyoSeq;

	public void validateUpdateRaitenYoin() {

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		//必須チェック
	}

	private void init() throws Exception{
		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);
	}

	@Override
	public String execute() throws Exception{

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{
			//画面表示内容の設定
			hyojiJunbi(con);
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	/**
	 * 売上種別追加
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String add() throws Exception{

		init();

		for(UriageShubetsuBean uriageShubetsuBean : this.uriageShubetsuBeanList) {
			uriageShubetsuBean.setStrGroupCod(bSWSSession.getStrGroupCod());
			uriageShubetsuBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
		}

		//売上種別行追加
		UriageShubetsuBean uriageShubetsuBean = new UriageShubetsuBean();
		uriageShubetsuBean.setStrGroupCod(bSWSSession.getStrGroupCod());
		uriageShubetsuBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
		this.uriageShubetsuBeanList.add(uriageShubetsuBean);

		return SUCCESS;
	}

	public String updateUriageShubetsu() throws Exception{

		UriageShubetsuDao uriageShubetsuDao = UriageShubetsuDao.getInstance();

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{

			//売上種別情報更新対象取得
			for(UriageShubetsuBean uriageShubetsuBean : uriageShubetsuBeanList) {
				int cnt = 0;
				if(uriageShubetsuBean.getSeq() != null){
					//情報更新
					cnt = uriageShubetsuDao.updateUriageShubetsu(con, bSWSSession, uriageShubetsuBean);
				}else{
					if(BSWSUtils.isNotBlankNull(uriageShubetsuBean.getStrUriageShubetsuKnj())
							&& BSWSUtils.isNotBlankNull(uriageShubetsuBean.getStrUriageShubetsuRyakuKnj())
							&& BSWSUtils.isNotBlankNull(uriageShubetsuBean.getStrUriageShubetsuHyojiJun())
							&& BSWSUtils.isNotBlankNull(uriageShubetsuBean.getStrUriageShubetsuHyojiFlg())
					){
						//情報追加
						cnt = uriageShubetsuDao.insertUriageShubetsu(con, null, bSWSSession, uriageShubetsuBean);
					}
				}
				if(cnt == 0){
					//処理なし
				}
			}

			//画面表示内容の設定
			hyojiJunbi(con);

			con.commit();
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	public String deleteUriageShubetsu() throws Exception{

		BSWSDao bSWSDao = BSWSDao.getInstance();

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{
			//売上種別削除
			int cnt = bSWSDao.deleteRow(con, intTaisyoSeq, " M_URIAGE_SHUBETSU ");
			if(cnt == 0){
				//処理なし
			}

			//画面表示内容の設定
			hyojiJunbi(con);

			con.commit();
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	private void hyojiJunbi(Connection con) throws Exception{

		//売上種別マスタ情報を取得
		getUriageShubetsu(con);

		//売上種別行追加
		UriageShubetsuBean uriageShubetsuBean = new UriageShubetsuBean();
		uriageShubetsuBean.setStrGroupCod(bSWSSession.getStrGroupCod());
		uriageShubetsuBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
		uriageShubetsuBeanList.add(uriageShubetsuBean);
	}

	private void getUriageShubetsu(Connection con) throws Exception{

		UriageShubetsuDao uriageShubetsuDao = UriageShubetsuDao.getInstance();
		this.uriageShubetsuBeanList = new ArrayList<UriageShubetsuBean>();
		this.uriageShubetsuBeanList.clear();

		//売上種別マスタ情報を取得
		List<UriageShubetsuModel> uriageShubetsuList = uriageShubetsuDao.getUriageShubetsuJoho(con, null, bSWSSession.getStrGroupCod(), null);
		for(int i = 0; i < uriageShubetsuList.size(); i++){
			UriageShubetsuBean uriageShubetsuBean = new UriageShubetsuBean();
			uriageShubetsuBean.setSeq(uriageShubetsuList.get(i).getSeq());
			uriageShubetsuBean.setStrGroupCod(bSWSSession.getStrGroupCod());
			uriageShubetsuBean.setStrUriageShubetsuKnj(uriageShubetsuList.get(i).getStrUriageShubetsuKnj());
			uriageShubetsuBean.setStrUriageShubetsuRyakuKnj(uriageShubetsuList.get(i).getStrUriageShubetsuRyakuKnj());
			uriageShubetsuBean.setStrUriageShubetsuHyojiJun(String.valueOf(uriageShubetsuList.get(i).getIntUriageShubetsuHyojiJun()));
			uriageShubetsuBean.setStrUriageShubetsuHyojiFlg(uriageShubetsuList.get(i).getStrUriageShubetsuHyojiFlg());
			uriageShubetsuBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
			this.uriageShubetsuBeanList.add(uriageShubetsuBean);
		}
	}

	public BSWSSession getbSWSSession() {
		return bSWSSession;
	}

	public void setbSWSSession(BSWSSession bSWSSession) {
		this.bSWSSession = bSWSSession;
	}

	public List<UriageShubetsuBean> getUriageShubetsuBeanList() {
		return uriageShubetsuBeanList;
	}

	public void setUriageShubetsuBeanList(
			List<UriageShubetsuBean> uriageShubetsuBeanList) {
		this.uriageShubetsuBeanList = uriageShubetsuBeanList;
	}

	public Integer getIntTaisyoSeq() {
		return intTaisyoSeq;
	}

	public void setIntTaisyoSeq(Integer intTaisyoSeq) {
		this.intTaisyoSeq = intTaisyoSeq;
	}

}